# -*- coding:utf-8 -*-
__author__ = 'xigu007'
from  kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.metrics import dp
from kivy.properties import BooleanProperty,NumericProperty,StringProperty, OptionProperty,ListProperty
from composite_widget.base import RelativeComposite, Division
from composite_widget.content import ShapeContent
from composite_widget.animate import register_animate

keyframes = [(0.0, {'rotate':{'angle':120, 'origin': (0.5, 0.5)}, 'opacity':1.0,  'time_func':'ease-out'}),\
             (0.08, {'rotate':{'angle':0}, 'opacity':1.0, 'time_func':'linear'}),\
             (0.3, {'rotate':{'angle':-110}, 'time_func':'ease-in-out'}),\
             (0.4, {'rotate':{'angle':-345}, 'time_func':'linear'}),\
             (0.7, {'rotate':{'angle':-470}, 'time_func':'ease-out'}),\
             (0.75, {'rotate':{'angle':-600}, 'time_func':'ease-out'}),\
             (0.77, {'rotate':{'angle':-600}, 'opacity':0.0}),\
             (1.0, {'rotate':{'angle':-600}, 'opacity':0.0})]
win_ball = {'duration':3.63,  'keyframes': keyframes}            #3.632
register_animate('win_ball', **win_ball)

class Win10LoadingDiv(Division):
    def __init__(self, **kwargs):
        self.balls = []
        self.ball_size = kwargs.get('ball_size', dp(20))
        self.radius_center = kwargs.get('radius_center', 0.8)
        self.ball_color = kwargs.get('ball_color', (1.0,0.2,0.7,1))
        super(Win10LoadingDiv,self).__init__(**kwargs)
        self.contents_typesetting = 'float'
        self._create()

    def _create(self):
        for i in range(5):
            ball = ShapeContent(shape_size=self.ball_size, ellipse=True, color=self.ball_color)
            ball.pos_hint = (0.5*(1.0 - self.radius_center), 0.5)
            ball.transform = {'rotate':{'angle':120, 'origin': (0.5, 0.5)}}
            self.balls.append(ball)
            self.add_widget(ball)

    def bind_ids(self, composite):
        for i, ball in enumerate(self.balls):
            ball_id = 'ball{}'.format(i)
            #把ball content添加到root（组合控件）的ids里，因为动画是通过ids来绑定的
            composite.bind_content_id(ball, ball_id)
            if  0 == i:
                composite.bind_animate(ball_id, 'win_ball', None, 0.153)
            elif 1 == i:
                composite.bind_animate(ball_id, 'win_ball',None, 0.316)
            elif 2 == i:
                composite.bind_animate(ball_id, 'win_ball',None, 0.469)
            elif 3 == i:
                composite.bind_animate(ball_id, 'win_ball',None, 0.633)
            elif 4 == i:
                composite.bind_animate(ball_id, 'win_ball',None, 0.796)

    def start(self, composite):
        for i, ball in enumerate(self.balls):
            ball_id = 'ball{}'.format(i)
            composite.start_animate(ball_id, 100)

    def stop(self, composite):
        for i, ball in enumerate(self.balls):
            ball_id = 'ball{}'.format(i)
            composite.stop_animate(ball_id)

class Win10Loading(RelativeComposite):
    ball_size = ListProperty([dp(20),dp(20)])
    radius_center = NumericProperty(0.4)
    ball_color = ListProperty([0.7,0.7,0.7,1])
    def __init__(self, **kwargs):
        super(Win10Loading, self).__init__(**kwargs)
        self.win10_loading = Win10LoadingDiv(ball_size=self.ball_size, radius_center=self.radius_center)
        self.add_widget(self.win10_loading)
        self.win10_loading.bind_ids(self)

    def start(self):
        if self.win10_loading:
            self.win10_loading.start(self)

    def stop(self):
        if self.win10_loading:
            self.win10_loading.stop(self)

if __name__ == '__main__':
    from kivy.app import App
    from kivy.clock import Clock
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.button import Button
    from kivy.logger import Logger
    class MyApp(App):
        def build(self):
            root = BoxLayout(orientation='vertical')
            self.loading = Win10Loading()
            root.add_widget(self.loading)
            bottom = BoxLayout(size_hint_y= None, height=40)
            root.add_widget(bottom)
            bottom.add_widget(Button(text='start', on_release=self.button1_click))
            bottom.add_widget(Button(text='stop', on_release=self.button2_click))
            #Clock.schedule_interval(self._show_fps, .7)
            return root

        def button1_click(self,*args):
            self.loading.start()

        def button2_click(self,*args):
            self.loading.stop()

        def _show_fps(self, dt):
            Logger.info('HappyValley: FPS={0}'.format(Clock.get_fps()))

    MyApp().run()